﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using KBS8.Shared;

namespace KBS8.Data.GdfDataFilter
{
    class LineFeatureReader: DatabaseReader
    {
        private string _query;
        public LineFeatureReader(SqlCeConnection connection) : base(connection)
        {
            
        }

        /// <summary>
        /// Reads the LineFeatures in the database
        /// </summary>
        /// <param name="o"></param>
        public override void Read(FilteredOutput o)
        {
            _query = @" SELECT lf.LineFeatureId, lf.FeatureDefenitionId, lf.MaxX, lf.MaxY, lf.MinX, lf.MinY " + 
                    "FROM LineFeature AS lf " +
                    "WHERE (" + o.Area.X + " < MaxX) AND " +
                    "(MinX < " + (o.Area.X + o.Area.rec.Width) + ") AND " +
                    "(" + o.Area.Y + " < MaxY) AND " +
                    "(MinY < " + o.Area.Y + o.Area.rec.Height + ")";

            Connection.Open();
            SqlCeCommand command = Connection.CreateCommand();
            command.CommandText = _query;
            SqlCeDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                LineFeature tmp = new LineFeature();
                tmp.LineId = Convert.ToInt32(reader["LineFeatureId"]);
                tmp.FeatureId = Convert.ToInt32(reader["FeatureDefenitionId"]);
                tmp.MaxX = Convert.ToInt32(reader["MaxX"]);
                tmp.MinX = Convert.ToInt32(reader["MinX"]);
                tmp.MaxY = Convert.ToInt32(reader["MaxY"]);
                tmp.MinY = Convert.ToInt32(reader["MinY"]);
                o.LineFeatures.Add(tmp);
            }

            Connection.Close();
        }
    }
}
